//初始化兑换详情列表
var lpid = manager.findParamFromUrl('lpid'),//礼品id
    ywy  = manager.findParamFromUrl('ywy');//业务域
var qrdhInterface = function() {
	// 获取全部礼品数据
	function getQrdhDta(fn,url) {	
		$.ajax({
			type : "get",
			url : Constants.csUrlPath + url ,
			success : function(data) {
				fn.call("", data);
			}
		});
	}
	//加载数据
	function load() {
		var dzUrl = '/lpdh/getShdz';
		var ractiveShdz = new Ractive({
			el : '#dzInfo',
			template : "#shdz-template",
			oncomplete : function() {
				// 设置收货地址数据
				getQrdhDta(function(data) {
                    if(data){
                    	var datap=data;
                    	datap['dzbmzh']=getString(data.dzbm,5);
                    	datap['shrzh']=getString(data.shr,5);
                    	datap['shxxdzzh']=getString(data.shxxdz,20);
    					ractiveShdz.set("qblp", datap);
    					ractive.set('imgUrl',Constants.imgUrl);
    					$('.glshdz').attr('data-id',data.id);
                    }
				},dzUrl);
			}
		});
		var dhlistUrl = 'lpdh/getLpdhxq?lpid=' + lpid +'&ywy=' + ywy;
		var ractive = new Ractive({
			el : '#qrdhContainerList',
			template : "#qrdhList-template",
			oncomplete : function() {
				// 设置收货地址数据
				getQrdhDta(function(data) {
					ractive.set("qrdhList", data);
					ractive.set('imgUrl',Constants.imgUrl);
					if(data.lpmc&&getString!='undefined'){
						ractive.set('lpmc',getString(data.lpmc,10));	
					}
					ractive.set("dhywy", ywy);
				},dhlistUrl);
			}
		});
	}
	//返回指定长度的字符串
	function getString(s,len){
	    if(s.length > len){
	        return s.substring(0,len)+'...';
	    }
	    return s; 
	}  
	function inputChange(){
		console.log(99);
	}
	//点击管理收货地址弹框
	$('.glshdz').click(function(){
		var shdzId = $('.glshdz').attr('data-id');
		var buttons = [{
			"buttonName": '保存',
			"buttonFunction": function() { //发布
			 //初始化掉各提示
			 $('#shrtip').addClass('none');
			 $('#yxtip').addClass('none');
			 $('#teltip').addClass('none');
			 $('#shdztip').addClass('none');
			 //收货人验证
              var shrInput = $('.shrInput').val().trim();//收货人
              if (!shrInput){
            	  $('#shrtip').removeClass('none');
            	  return;
              }else{
            	  $('#shrtip').addClass('none');
              }
             //邮箱验证
              var yxInput  = $('.yxInput').val().trim();//邮箱
              if (yxInput && !/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(yxInput)){
            	  $('#yxtip').removeClass('none');
            	  $('#yxtip').text('邮箱格式有误！');
            	  return;
              }else{
            	  $('#yxtip').addClass('none');
              }
             //手机号码验证
              var telInput  = $('.telInput').val().trim();//手机号码
              var gddhInput=$('.gddhInput').val().trim();//固定电话
              if (telInput && !/^1[34578]\d{9}$/.test(telInput)){
            	  $('#teltip').removeClass('none');
            	  $('#teltip').text('手机号码格式有误！'); 
            	  return;
              }else if(!telInput){
            	  $('#teltip').removeClass('none');
            	  $('#teltip').text('手机号码不能为空！'); 
            	  return;
              }else if(gddhInput&&!/^((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(gddhInput)){
            	  $('#gddhtip').removeClass('none');
            	  $('#gddhtip').text('固定电话格式有误！'); 
            	  return;
              }else{
            	  $('#teltip').addClass('none'); 
              }
             //收货地址验证
              var shdzInput = $('.shdzInput').val().trim();//收货地址
              if (!shdzInput){
            	  $('#shdztip').removeClass('none');
            	  $('#shdztip').text('收货地址不能为空！'); 
            	  return;
              }
              else{
            	  $('#shdztip').addClass('none');
              }
              //构造请求接口参数
            	var param = {//post请求参数
		    		shxxdz:shdzInput,
		    		shr:shrInput,
		    		shrsjhm: telInput,
		    		shrdhhm: $('.gddhInput').val().trim(),
		    		yx: yxInput,
		    		dzbm: $('.dzbmInput').val().trim(),
		    		ywy:ywy
		    	}
            	if (shdzId){
            		param.id = shdzId;
            	}
            	AddAdrressList(param);
			}
		}]
		var nowParam = { 
			innerhtml:$('#glshdz-template').html(),
			title: '编辑收货地址',
			width:618,
			height:457,
			buttons:buttons
		}
		dialog(nowParam);
        //地址别名填充数据
  		var dzbmUrl = "common/getCodeListByCodeClass?codeClass="+'DZBM';
		getQrdhDta(function(data) {
			var str ='';
			if(data){
				for(var item in data){
					str += '<i>'+ data[item].codedesc + '</i>';
				}
				$(".dzbmList").html(str);
			}
		},dzbmUrl);
		var dzUrl = '/lpdh/getShdz';
		if (shdzId){
			getQrdhDta(function(data){
				$(".shrInput").val(data.shr);
				$(".yxInput").val(data.yx);
				$(".telInput").val(data.shrsjhm);
				$(".gddhInput").val(data.shrdhhm);
				$(".shdzInput").val(data.shxxdz);
				$(".dzbmInput").val(data.dzbm);
			},dzUrl)
		}
	})
	function AddAdrressList(param){
		$.ajax({
			type : "post",
			url :Constants.csUrlPath + 'lpdh/updateShdz',
			data : JSON.stringify(param),
			dataType : "json",
			contentType : "application/json",
			async : false,
			success : function(data) {//数据结果
		     if(!data.result){
		    	 return;
		     }
		   //成功提示：
			var objs = {txt:'保存成功',callBackFun:fn}
			success(objs);
			function fn() {window.location.reload()}
			}
	    })
	}
	$('.ljdh').on('click',function(){
		if (!$('.glshdz').attr('data-id')){
			alert({txt:'请添加收货地址！'});
			return;
		}
		confirm({txt:'确认兑换吗？',successCallBackFun:function(){
			var param ={};
			param.lpid = lpid;
			param.dhlb ='00';
			param.shxxdz = $('.dzShdz').attr('title');
			param.shr = $('.dzName').attr('title');
			param.shrdhhm = $('.dzTel').text();
			param.ywy = ywy;
			$.ajax({
				type : "post",
				url :Constants.csUrlPath + 'lpdh/updateLjdh',
				data : JSON.stringify(param),
				dataType : "json",
				contentType : "application/json",
				async : false,
				success : function(data) {//数据结果
			     if(!data.result){
			    	 manager.errorMsg(data);
			    	 return;
			     }
			   //成功提示：
				var objs = {txt:'兑换成功！',callBackFun:fn}
				success(objs);
				function fn() {window.location.href= Constants.wddhUrl;}
				}
		    })
		},cancelCallBackFun:function(){
			return;
		}});

	})
	//地址别名点击事件
	$(document).delegate('i','click',function(){
		var curVal = $(this).text();
		$('.dzbmInput').val(curVal);
	})
   return {
		init : function() {
			load();
		}

	}
}();
$(document).ready(qrdhInterface.init);
